﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using DreamBuilder.Common;
using System.Data;

namespace Web.SystemConfig.DataAccess
{
    public class DALUpdateUserPasswd : DataAccessBase
    {
        internal System.Data.DataTable SelectStructedDepartments()
        {
            if (DBHelper.database is Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase)
            {
                string strSql = @"SELECT t.org_name,t.org_id,level 
                           FROM p_auth_org T
                          START WITH ORG_ID =(select t.org_id from p_auth_org t where t.parent_id is null)
                          CONNECT BY PRIOR  T.Org_Id= T.PARENT_ID";
                DataTable dt = DBHelper.ExecuteDataSet(strSql).Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    if (!string.IsNullOrEmpty(dr["LEVEL"].ToString()))
                    {
                        dr["org_name"] = "　|--" + dr["org_name"];
                        for (int i = int.Parse(dr["LEVEL"].ToString()); i > 0; i--)
                        {
                            dr["org_name"] = '　' + dr["org_name"].ToString();
                        }
                    }
                }
                dt.Columns.Remove("level");
                return dt;
            }
            else
            {
                DbParameter[] param = new DbParameter[] { };
                return DBHelper.RunProcedureDataTable("sp_selectStructedOrg", param);
            }
        }

        internal System.Data.DataTable SelectUserInfoByDepID(string depID)
        {
            string sSql = "";
            if (DBHelper.database is Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase)
            {
                if (string.IsNullOrEmpty(depID))
                {
                    sSql = @"SELECT DISTINCT P_Auth_User.User_ID,P_Auth_User.User_Name || '('|| P_Auth_ORG.ORG_Name || ')' AS FullName  
                        FROM P_Auth_User,P_Auth_ORG_User,P_Auth_ORG   
                        where   P_Auth_ORG_User.ORG_ID =P_Auth_ORG.ORG_ID  
                        AND P_Auth_User.User_ID=P_Auth_ORG_User.User_ID  
                        order by P_Auth_User.User_ID ";
                }
                else
                {
                    sSql = @"SELECT DISTINCT P_Auth_User.User_ID,P_Auth_User.User_Name  || '(' || P_Auth_ORG.ORG_Name || ')' AS FullName  
                        FROM P_Auth_User,P_Auth_ORG_User,P_Auth_ORG   
                        where    
                        P_Auth_ORG_User.ORG_ID =P_Auth_ORG.ORG_ID  
                        AND  
                        P_Auth_User.User_ID=P_Auth_ORG_User.User_ID  ";
                    sSql += " AND P_Auth_ORG_User.ORG_ID = '" + depID + "'";
                    sSql += " order by P_Auth_User.User_ID";
                }
            }
            else
            {
                if (string.IsNullOrEmpty(depID))
                {
                    sSql = @"SELECT DISTINCT P_Auth_User.User_ID,P_Auth_User.User_Name+'('+ P_Auth_ORG.ORG_Name+')' AS FullName  
                        FROM P_Auth_User,P_Auth_ORG_User,P_Auth_ORG   
                        where   P_Auth_ORG_User.ORG_ID =P_Auth_ORG.ORG_ID  
                        AND P_Auth_User.User_ID=P_Auth_ORG_User.User_ID  
                        order by P_Auth_User.User_ID ";
                }
                else
                {
                    sSql = @"SELECT DISTINCT P_Auth_User.User_ID,P_Auth_User.User_Name+'('+ P_Auth_ORG.ORG_Name+')' AS FullName  
                        FROM P_Auth_User,P_Auth_ORG_User,P_Auth_ORG   
                        where    
                        P_Auth_ORG_User.ORG_ID =P_Auth_ORG.ORG_ID  
                        AND  
                        P_Auth_User.User_ID=P_Auth_ORG_User.User_ID  ";
                    sSql += " AND P_Auth_ORG_User.ORG_ID = '" + depID + "'";
                    sSql += " order by P_Auth_User.User_ID";
                }
            }
            return DBHelper.ExecuteDataSet(sSql).Tables[0];
        }

        internal int UpdateUserPasswd(string userID, string pass)
        {
            string strSql = "UPDATE P_Auth_User SET User_Pwd = '" + pass + "' WHERE User_ID = '" + userID + "'";
            return DBHelper.ExecuteSql(strSql);
        }
    }
}
